<?php
// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;

class UserStars extends Model
{
	use \base\model\ModelTrait;
	//店铺，一对一关联
	public function shopInfo(){
        return $this->hasOne('shop','id','shop_id')->bind([
        	'shop_name'=>'name',
        	'shop_logo'=>'img',
        ]);
    }

	/**
    * 验证规则
    */
	protected $rule = [
		'uid|用户id'=>'require|integer',
		'shop_id|店铺id'=>'require|integer',
		'goods_id|商品id'=>'require|integer',
    	'type|类型' => 'require'
	];
	
	/**
     * 用户收藏list
     * @param type $ids
     * @param type $v
     * @return type
     */
	public function userCollectList($uid,$type=0)
    {
    	$model = new self;
		$model = $model->with('shopInfo');
		$model = $model->where('type','collect');
		$model = $model->where('uid',$uid);
		if($type==1){
			$model = $model->where('goods_id','<>',0);
		}
		if($type==2){
			$model = $model->where('goods_id',0);
		}
		$model = $model->field('add_time',true);
		$model = $model->page(input('pageIndex') ?: 1,input('pageSize') ?: 20);
		$model = $model->group('goods_id');
    	$res = $model->select()->toArray();
		foreach($res as $key=>$val){
			if( !$res[$key]['goodsInfo'] = model("goods")->getModel()->get($val['goods_id']) ){
				unset($res[$key]);
			}
		}
		return array_values($res);
    }
	
	/**
     * 取消收藏
     * @param type $ids
     * @param type $v
     * @return type
     */
	public function delCollect($uid,$goods_id)
    {
    	return $this->where('uid',$uid)
    		->where('type','collect')
    		->where('goods_id',$goods_id)
    		->delete();
    }
	
	/**
     * 取消店铺收藏
     * @param type $ids
     * @param type $v
     * @return type
     */
	public function delShopCollect($uid,$shop_id)
    {
    	return $this->where('uid',$uid)
    		->where('type','collect')
    		->where('shop_id',$shop_id)
    		->delete();
    }
	
	/**
     * 取消点赞
     * @param type $ids
     * @param type $v
     * @return type
     */
	public function delLike($uid,$goods_id)
    {
    	return $this->where('uid',$uid)->where('type','like')->where('goods_id',$goods_id)->delete();
    }
	
}
